//
//  GetCountryRequest.m
//  fashionbuyer
//
//  Created by ale tan on 2018/1/17.
//  Copyright © 2018年 ale tan. All rights reserved.
//

#import "GetCountryRequest.h"
#import "FFCache.h"
#import "NSString+Utils.h"

#define Countries @"Countries"

@implementation GetCountryRequest

- (id)init{
    self = [super init];
    if (self) {
        self.countriesUrl = @"platform/v1/address/country/code";
    }
    return self;
}

- (void)requestForCountiesWithCallBack:(RequestResult)callback{
    self.requestCallBack = callback;
    
    NSArray *array = [FFCache queryCache:Countries];
    if (array.count) {
        self.requestCallBack(array,YES);
    }
    else{
        [ServiceManager postTrans:self.countriesUrl withRequest:self.parameters success:^(NSDictionary *dictResponse, BOOL result) {
            if (result) {
                
                NSMutableArray *datas = [NSMutableArray array];
                NSArray *letterArr = @[@"#",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
                for (int i=0; i<letterArr.count; i++) {
                    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:letterArr[i],@"index",[NSMutableArray array],@"names", nil];
                    [datas addObject:dic];
                }
                
                for (NSDictionary *dic in dictResponse[@"data"][@"country_code"]) {
                    FBCountryModel *model = [[FBCountryModel alloc] initWithDictionary:dic];
                    
                    //添加到热门
                    if (model.is_hot) {
                        NSMutableArray *array = datas[0][@"names"];
                        [array addObject:model];
                    }
                    
                    //添加到对应字母
                    NSString *firstChar = [[[NSString convertToPinYin:model.chinese_name] substringToIndex:1] uppercaseString];
                    NSLog(@"%d",[firstChar characterAtIndex:0]);
                    NSArray *array = datas[[firstChar characterAtIndex:0]-64][@"names"];
                    NSMutableArray *newArray = [NSMutableArray arrayWithArray:array];
                    [newArray addObject:model];
                    
                    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:firstChar,@"index",newArray,@"names", nil];
                    [datas replaceObjectAtIndex:[firstChar characterAtIndex:0]-64 withObject:dic];
                }
                
                //去除没有数据的字母
                for (NSInteger j=datas.count-1;j>=0;j--) {
                    NSDictionary *dic = datas[j];
                    NSArray *array = dic[@"names"];
                    if (!array.count) {
                        [datas removeObject:dic];
                    }
                }
                
                //缓存
                [FFCache addCache:Countries obj:datas];
                
                self.requestCallBack(datas,YES);
            }
            else{
                [FFTipsView showTips:dictResponse[@"toast"]];
                self.requestCallBack(dictResponse,NO);
            }
            
        } failure:^(NSError *error) {
            [FFTipsView showTips:@"网络连接失败"];
            self.requestCallBack(nil,NO);
        }];
    }
}

@end
